#import "RNToolbarViewController.h"
#import "RNToolbar.h"
#import "RNToolbarButton.h"
#import "MainWindowController.h"
#import "DetailViewController.h"
#import "RNList.h"
#import "RNItem.h"
#import "RNListItem.h"
#import "RNConstants.h"

NSString* const RNToolbarButtonImageKey = @"image";
NSString* const RNToolbarButtonTitleKey = @"title";
NSString* const RNToolbarButtonActionKey = @"action";

NSString* const RNAddToolbarButtonKey = @"RNAddToolbarButtonKey";
NSString* const RNEditToolbarButtonKey = @"RNEditToolbarButtonKey";
NSString* const RNFavoriteToolbarButtonKey = @"RNFavoriteToolbarButtonKey";
NSString* const RNNewItemToolbarButtonKey = @"RNNewItemToolbarButtonKey";
NSString* const RNNewAisleToolbarButtonKey = @"RNNewAisleToolbarButtonKey";
NSString* const RNNewCategoryToolbarButtonKey = @"RNNewCategoryToolbarButtonKey";
NSString* const RNClearCompletedToolbarButtonKey = @"RNClearCompletedToolbarButtonKey";

static NSArray* defaultToolbarIdentifiers;
static NSArray* standardItemSpecificToolbarIdentifiers;

@implementation RNToolbarViewController

@synthesize windowController = windowController_;
@synthesize toolbar = toolbar_;
@synthesize toolbarButtonsByIdentifier = toolbarButtonsByIdentifier_;
@synthesize newThingIdentifiersByClassname = newThingIdentifiersByClassname_;

+(void)initialize {
    defaultToolbarIdentifiers = [NSArray arrayWithObjects:RNNewItemToolbarButtonKey, RNToolbarFlexibleSpaceButtonIdentifier, RNEditToolbarButtonKey, RNFavoriteToolbarButtonKey, RNToolbarFlexibleSpaceButtonIdentifier, nil];
    standardItemSpecificToolbarIdentifiers = [NSArray arrayWithObjects:RNToolbarFlexibleSpaceButtonIdentifier, RNFavoriteToolbarButtonKey, RNEditToolbarButtonKey, RNToolbarFlexibleSpaceButtonIdentifier, nil];
}

-(void)awakeFromNib {
    // Initialize toolbar buttons and save them in a dictionary by identifier
    RNToolbarButton *addButton = [[RNToolbarButton alloc] initWithIdentifier:RNAddToolbarButtonKey withTitle:@"Add" withImageNamed:@"toolbar_add.png" withAction:@selector(displayAddListItemSheet:)];
    RNToolbarButton *editButton = [[RNToolbarButton alloc] initWithIdentifier:RNEditToolbarButtonKey withTitle:@"Edit" withImageNamed:@"toolbar_edit.png" withAction:@selector(editSelectedItem:)];
    RNToolbarButton *favoriteButton = [[RNToolbarButton alloc] initWithIdentifier:RNFavoriteToolbarButtonKey withTitle:@"Favorite" withImageNamed:@"toolbar_favorite.png" withAction:@selector(toggleFavoriteForSelectedItems:)];
    RNToolbarButton *newItemButton = [[RNToolbarButton alloc] initWithIdentifier:RNNewItemToolbarButtonKey withTitle:@"New Item" withImageNamed:@"toolbar_newItem.png" withAction:@selector(displayNewItemSheetAndSelectItem:)];
    RNToolbarButton *newAisleButton = [[RNToolbarButton alloc] initWithIdentifier:RNNewAisleToolbarButtonKey withTitle:@"New Aisle" withImageNamed:@"toolbar_newAisle.png" withAction:@selector(displayNewItemSheetAndSelectAisle:)];
    RNToolbarButton *newCategoryButton = [[RNToolbarButton alloc] initWithIdentifier:RNNewCategoryToolbarButtonKey withTitle:@"New Category" withImageNamed:@"toolbar_newCategory.png" withAction:@selector(displayNewItemSheetAndSelectCategory:)];
    RNToolbarButton *clearCompletedButton = [[RNToolbarButton alloc] initWithIdentifier:RNClearCompletedToolbarButtonKey withTitle:@"Clear Completed" withImageNamed:@"toolbar_history.png" withAction:@selector(clearCompletedItems:)];
    [self setToolbarButtonsByIdentifier:[NSDictionary dictionaryWithObjectsAndKeys:addButton, RNAddToolbarButtonKey, editButton, RNEditToolbarButtonKey, favoriteButton, RNFavoriteToolbarButtonKey, newItemButton, RNNewItemToolbarButtonKey, newAisleButton, RNNewAisleToolbarButtonKey, newCategoryButton, RNNewCategoryToolbarButtonKey, clearCompletedButton, RNClearCompletedToolbarButtonKey, nil]];
    
    // Set which "new thing" type items are used based on what is the current root object
    NSArray *addArray = [NSArray arrayWithObject:RNAddToolbarButtonKey];
    NSArray *newItemArray = [NSArray arrayWithObject:RNNewItemToolbarButtonKey];
    NSArray *newAisleArray = [NSArray arrayWithObject:RNNewAisleToolbarButtonKey];
    NSArray *newItemAndNewCategoryArray = [NSArray arrayWithObjects:RNNewItemToolbarButtonKey, RNNewCategoryToolbarButtonKey, nil];
    [self setNewThingIdentifiersByClassname:[NSDictionary dictionaryWithObjectsAndKeys:addArray, RNEntityNameList, newAisleArray, RNEntityNameStore, newItemAndNewCategoryArray, RNClassNameCatalog, newItemAndNewCategoryArray, RNEntityNameCategory, newItemArray, RNEntityNameAisle, newItemArray, RNClassNameFavorites, newItemArray, RNClassNameHistory, nil]];
    
    if ([[[self toolbar] buttons] count] == 0)
        [[self toolbar] updateToolbar];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidResignActive:) name:NSApplicationDidResignActiveNotification object:nil];
    [[self windowController] addObserver:self forKeyPath:@"rootObject" options:NSKeyValueObservingOptionNew context:NULL];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detailViewSelectionDidChange:) name:NSOutlineViewSelectionDidChangeNotification object:[[[self windowController] detailViewController] detailView]];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    [[self toolbar] updateToolbar];
}


#pragma mark -
#pragma mark Toolbar delegate methods

-(RNToolbarButton *)toolbar:(RNToolbar *)toolbar toolbarButtonForIdentifier:(NSString *)buttonIdentifier {
    return [[self toolbarButtonsByIdentifier] valueForKey:buttonIdentifier];
}

-(NSArray *)buttonIdentifiersForToolbar:(RNToolbar *)toolbar {
    id rootObject = [[self windowController] rootObject];
    if (!rootObject)
        return defaultToolbarIdentifiers;
    NSArray *newThingArray = [[self newThingIdentifiersByClassname] valueForKey:[rootObject className]];
    if (!newThingArray)
        return defaultToolbarIdentifiers;
    NSMutableArray *identifiers = [NSMutableArray arrayWithArray:newThingArray];
    [identifiers addObjectsFromArray:standardItemSpecificToolbarIdentifiers];
    if ([rootObject isKindOfClass:[RNList class]])
        [identifiers addObject:RNClearCompletedToolbarButtonKey];
    return identifiers;
}


#pragma mark -
#pragma mark Notifications

-(void)appDidResignActive:(NSNotification *)note {
    [[self toolbar] validateVisibleItems];
}

-(void)detailViewSelectionDidChange:(NSNotification *)note {
    [[self toolbar] updateToolbar];
}

@end
